//
//  DemoCATransitionPresentedViewController.m
//  HDTransitonDemo
//
//  Created by mac on 2017/5/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "DemoCATransitionPresentedViewController.h"
#import "UINavigationController+LiDaPush.h"
@interface DemoCATransitionPresentedViewController ()<UIGestureRecognizerDelegate>{
    UIPercentDrivenInteractiveTransition *_interactiveTransition;
}

@end

@implementation DemoCATransitionPresentedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"02" ofType:@"jpg"]];
    [self.view addSubview:imageView];

    [self addBtns];
    
    [self panToPop];
}
- (void)addBtns{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width, 50)];
    [btn setTitle:@"pop" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(actionButton1:) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.borderColor = [UIColor redColor].CGColor;
    btn.layer.borderWidth = 2.0f;
    [self.view addSubview:btn];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 220, self.view.frame.size.width, 50)];
    [btn2 setTitle:@"dismiss" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [btn2 addTarget:self action:@selector(actionButton2:) forControlEvents:UIControlEventTouchUpInside];
    btn2.layer.borderColor = [UIColor redColor].CGColor;
    btn2.layer.borderWidth = 2.0f;
    [self.view addSubview:btn2];
}

- (void)actionButton2:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    return;
    switch (_demoType) {
            // 常见四种
        case DemoCATransitionTypeFade:
            [self.navigationController LiDa_DissWithAnimationType:kCATransitionFade WithSubtype:nil];
            break;
        case DemoCATransitionTypePush:
            [self.navigationController LiDa_DissWithAnimationType:kCATransitionPush WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeMoveIn:
            [self.navigationController LiDa_DissWithAnimationType:kCATransitionMoveIn WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeReveal:
            [self.navigationController LiDa_DissWithAnimationType:kCATransitionReveal WithSubtype:kCATransitionFromLeft];
            break;
            
        case DemoCATransitionTypeCube:
            [self.navigationController LiDa_DissWithAnimationType:@"cube" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeOglFlip:
            [self.navigationController LiDa_DissWithAnimationType:@"oglFlip" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeSuckEffect:
            [self.navigationController LiDa_DissWithAnimationType:@"suckEffect" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeRippleEffect:
            [self.navigationController LiDa_DissWithAnimationType:@"suckEffect" WithSubtype:nil];
            break;
        case DemoCATransitionTypePageCurl:
            [self.navigationController LiDa_DissWithAnimationType:@"pageCurl" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypePageUnCurl:
            [self.navigationController LiDa_DissWithAnimationType:@"pageUnCurl" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeCameraIrisHollowOpen:
            [self.navigationController LiDa_DissWithAnimationType:@"cameraIrisHollowOpen" WithSubtype:nil];
            break;
        case DemoCATransitionTypeCameraIrisHollowClose:
            [self.navigationController LiDa_DissWithAnimationType:@"cameraIrisHollowClose" WithSubtype:nil];
            break;
        default:
            break;
    }

}

- (void)actionButton1:(UIButton *)sender {
    
    switch (_demoType) {
            // 常见四种
        case DemoCATransitionTypeFade:
            [self.navigationController LiDa_PopWithAnimationType:kCATransitionFade WithSubtype:nil];
            break;
        case DemoCATransitionTypePush:
            [self.navigationController LiDa_PopWithAnimationType:kCATransitionPush WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeMoveIn:
            [self.navigationController LiDa_PopWithAnimationType:kCATransitionMoveIn WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeReveal:
            [self.navigationController LiDa_PopWithAnimationType:kCATransitionReveal WithSubtype:kCATransitionFromLeft];
            break;
            
        case DemoCATransitionTypeCube:
            [self.navigationController LiDa_PopWithAnimationType:@"cube" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeOglFlip:
            [self.navigationController LiDa_PopWithAnimationType:@"oglFlip" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeSuckEffect:
            [self.navigationController LiDa_PopWithAnimationType:@"suckEffect" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeRippleEffect:
            [self.navigationController LiDa_PopWithAnimationType:@"suckEffect" WithSubtype:nil];
            break;
        case DemoCATransitionTypePageCurl:
            [self.navigationController LiDa_PopWithAnimationType:@"pageCurl" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypePageUnCurl:
            [self.navigationController LiDa_PopWithAnimationType:@"pageUnCurl" WithSubtype:kCATransitionFromLeft];
            break;
        case DemoCATransitionTypeCameraIrisHollowOpen:
            [self.navigationController LiDa_PopWithAnimationType:@"cameraIrisHollowOpen" WithSubtype:nil];
            break;
        case DemoCATransitionTypeCameraIrisHollowClose:
            [self.navigationController LiDa_PopWithAnimationType:@"cameraIrisHollowClose" WithSubtype:nil];
            break;
        default:
            break;
    }
}


//滑动返回
- (void)panToPop{
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)handleNavigationTransition:(UIPanGestureRecognizer *)recognizer{
    //    [self.navigationController popViewControllerAnimated:YES];
    
    if(self.navigationController.childViewControllers.count == 1)
    {
        return;
    }
    // _interactiveTransition就是代理方法2返回的交互对象，我们需要更新它的进度来控制POP动画的流程。（以手指在视图中的位置与屏幕宽度的比例作为进度）
    CGFloat process = [recognizer translationInView:self.view].x/self.view.bounds.size.width;
    process = MIN(1.0, MAX(0.0, process));
    if(recognizer.state == UIGestureRecognizerStateBegan)
    {
        // 此时，创建一个UIPercentDrivenInteractiveTransition交互对象，来控制整个过程中动画的状态
        _interactiveTransition = [[UIPercentDrivenInteractiveTransition alloc] init];
        
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged)
    {
        [_interactiveTransition updateInteractiveTransition:process]; // 更新手势完成度
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded ||recognizer.state == UIGestureRecognizerStateCancelled)
    {
        // 手势结束时，若进度大于0.5就完成pop动画，否则取消
        if(process > 0.9)
        {
            [self.navigationController popViewControllerAnimated:YES];
            [_interactiveTransition finishInteractiveTransition];
        }
        else
        {
            [_interactiveTransition cancelInteractiveTransition];
        }
        
        _interactiveTransition = nil;
    }
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if(self.navigationController.childViewControllers.count == 1 || gestureRecognizer.view != self.view)
    {
        return NO;
    }
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
